Date: 2018-08-23
R version: 3.5.0
*Corresponding author: matthew.malishev@gmail.com
This document can be found at https://github.com/darwinanddavis/ggmap

TO DO:
- Fix Open Street Maps: osm code chunk

Overview

This document converts .json Google Maps data into useable R data for mining and plotting with ggmap and related packages. It also has links to obtaining Google API keys for using Google-protected data.

Working with Google Maps data

Links
Converting .json to .csv https://konklone.io/json/

Install packages

packages <- c("animation","RColorBrewer","dplyr","RgoogleMaps","sp","maptools","scales","rgdal","ggplot2","rjson")
if (require(packages)) {
  install.packages(packages,dependencies = T)
  require(packages)
  
# install RgoogleMaps and OpenStreetMap separately, for some reason  
  install.packages("RgoogleMaps"); library(RgoogleMaps)
  install.packages("OpenStreetMap"); library(OpenStreetMap)
  
# install ggmap and geojsonio from github and source
  devtools::install_github("dkahle/ggmap"); library(ggmap)
  devtools::install_github("ropensci/geojsonio"); library(geojsonio)
}


lapply(packages,library,character.only=T)

Set working dir

setwd(params$dir)

Weird Google things you need to do

Google has individual limits on who and how often one can indepedently use Google data, so in order to access and analyse your Google Maps data, you need to submit an Application Programming Interface (API) query to Google. Straightforward, but necessary.

 

Get API key: https://developers.google.com/maps/documentation/geocoding/get-api-key

ggmap features

  #### Functions

maptypes

maptypes_list <- list("terrain", "terrain-background", "satellite", "roadmap", "hybrid", "toner", "watercolor", "terrain-labels", "terrain-lines", "toner-2010", "toner-2011", "toner-background", "toner-hybrid", "toner-labels", "toner-lines", "toner-lite");maptypes_list
[[1]]
[1] "terrain"

[[2]]
[1] "terrain-background"

[[3]]
[1] "satellite"

[[4]]
[1] "roadmap"

[[5]]
[1] "hybrid"

[[6]]
[1] "toner"

[[7]]
[1] "watercolor"

[[8]]
[1] "terrain-labels"

[[9]]
[1] "terrain-lines"

[[10]]
[1] "toner-2010"

[[11]]
[1] "toner-2011"

[[12]]
[1] "toner-background"

[[13]]
[1] "toner-hybrid"

[[14]]
[1] "toner-labels"

[[15]]
[1] "toner-lines"

[[16]]
[1] "toner-lite"

sources

  • Google Maps: “google”
  • OpenStreetMap: “osm”
  • Stamen Maps: “stamen”
  • CloudMade maps: “cloudmade”
sources_list <- list("google","osm","stamen","cloudmade");print(sources_list)
[[1]]
[1] "google"

[[2]]
[1] "osm"

[[3]]
[1] "stamen"

[[4]]
[1] "cloudmade"

Plotting locations and geocodes

Enter city, map source, and map type

map <- "atlanta" 
zoom <- 15 # 10 = metropolitan level, 18 = street level. 12 is a useable city level
source <- "stamen"
maptype <- "toner-lines"
bg <- "pink" # set background color

Plotting

atl <- get_map(map, zoom=zoom, source = source, maptype = maptype)
# str(get_map("atl"))

# plot map  
ggmap(atl,
      legend="topright",
      extent="device",
      padding=0.5,
      darken=c(0.3,bg)
      )

# regular Google Map
paris <- get_map(location = "paris", zoom=15, source = "google", maptype = "hybrid")
ggmap(paris)

Geocodes

Geocode converts addresses to latlong coords link.
Get geocode parameters for a location

location <- "houston texas"
gc <- geocode(location, 
              output = "all", # "latlon", "latlona", "more", "all"
              source="google" # "google" or "dsk"
              ) 

Google only allows you a certain amount of memory to use its geocode data for each session. After this threshold, you can no longer call the data. To check how many geocode queries you have remaining from Google, type the following:

geocodeQueryCheck() 

Open Street Maps: osm

References

D. Kahle and H. Wickham. ggmap: Spatial Visualization with ggplot2. The R Journal, 5(1), 144-161. http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf